<?php

    require_once Core_IO_Path::combine(SERUM_MODULE_PATH, 'Base', 'controllers', 'admin', 'IndexController.php');
    
    /**
     * @package      Modules
     * @subpackage   Widgets_Controllers_Admin
	 *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Widgets_Controllers_Admin_Index extends Base_Controllers_Admin_Index
    {

	////////////////////////////////////////////////////////////////////////////
	//                                Public Methods                          //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Widgets list
         *
         * @return      Core_Controller_Response_Abstract
         */
        public function getList()
        {
            $rep            = $this->_getResponse('json');
            $widgets        = array();

            $admin          = Doctrine_Query::create()
                                ->from('DbAdmin a')
                                ->where('a.id = ?', Core_Authentification::getNamespace()->id)
                                ->fetchOne();

            $oWidgets        = Doctrine_Query::create()
                                ->from('DbWidget w')
                                ->where('w.status = ?', DbBase::ENABLED)
                                ->addWhere('w.enabled = ?', true)
                                ->addWhere('w.id IN (SELECT aw.widget_id FROM DbAdminWidget aw WHERE aw.admin_id = ? AND aw.display = ?)', array(Core_Authentification::getNamespace()->id, true))
                                ->execute();

            foreach ($oWidgets as $widget) {
                $processor  = new $widget->class_name();

                $widgets[]  = array(
                                    'id'        => $widget->id,
                                    'name'      => $widget->name,
                                    'class_name'=> $widget->class_name,
                                    'params'    => $processor->process($widget->getParams($admin))
                                   );
            }

            $rep->setResponseContent($widgets);
            return $rep;
        }

        /**
         * Add new widget
         *
         * @return      Core_Controller_Response_Abstract
         */
        public function add()
        {
            $rep            = $this->_getResponse('json');
            $fields         = array();
            $values         = array();

            $widgets        = Doctrine_Query::create()
                                ->from('DbWidget w')
                                ->where('w.status = ?', DbBase::ENABLED)
                                ->addWhere('w.enabled = ?', true)
                                ->addWhere('w.id NOT IN (SELECT aw.widget_id FROM DbAdminWidget aw WHERE aw.admin_id = ?)', Core_Authentification::getNamespace()->id)
                                ->execute();

            foreach ($widgets as $widget) {
                $values[] = array($widget->id, $widget->name);
            }

            
            $fields['widget'] = array(
                                        'type'      => 'relation1to1',
                                        'readonly'  => false,
                                        'fieldname' => 'widget',
                                        'values'    => $values);

            $rep->setResponseContent(array('fields' => array('default' => $fields)));
            return $rep;
        }

        /**
         * Save action
         *
         * @return      Core_Controller_Response_Abstract
         */
        public function saveAdd()
        {
            $admin          = Doctrine_Query::create()
                                ->from('DbAdmin a')
                                ->where('a.id = ?', Core_Authentification::getNamespace()->id)
                                ->fetchOne();

            $widget         = Doctrine_Query::create()
                                ->from('DbWidget w')
                                ->where('w.id = ?', $this->_intParam('rel-widget'))
                                ->fetchOne();

            if ($widget) {
                $widget->addForAdmin($admin);
            }

            $rep = $this->_getResponse('http');
            $rep->setTemplate('json.tpl');
            $rep->data->assign('json', Zend_Json::encode(array('success' => true, 'errors' => array())));
            return $rep;
        }

        /**
         * Delete action
         *
         * @return      Core_Controller_Response_Abstract
         */
        public function delete()
        {
            $admin          = Doctrine_Query::create()
                                ->from('DbAdmin a')
                                ->where('a.id = ?', Core_Authentification::getNamespace()->id)
                                ->fetchOne();

            $widget         = Doctrine_Query::create()
                                ->from('DbWidget w')
                                ->where('w.id = ?', $this->_intParam('widget'))
                                ->fetchOne();

            if ($widget) {
                $widget->deleteFromAdmin($admin);
            }

            $rep            = $this->_getResponse('json');
            return $rep;
        }

        /**
         * Edit widget
         *
         * @return      Core_Controller_Response_Abstract
         */
        public function edit()
        {
            $rep            = $this->_getResponse('json');
            $fields         = array();  

            $admin          = Doctrine_Query::create()
                                ->from('DbAdmin a')
                                ->where('a.id = ?', Core_Authentification::getNamespace()->id)
                                ->fetchOne();

            $widget         = Doctrine_Query::create()
                                ->from('DbWidget w')
                                ->leftJoin('w.DbAdminWidget aw')
                                ->where('w.status = ?', DbBase::ENABLED)
                                ->addWhere('w.enabled = ?', true)
                                ->addWhere('w.id = ?', $this->_intParam('elementId'))
                                ->addWhere('aw.admin_id = ?', Core_Authentification::getNamespace()->id)
                                ->fetchOne();

            if ($widget) {
                $params         = Doctrine_Query::create()
                                    ->from('DbWidgetParam wp')
                                    ->where('wp.status = ?', DbBase::ENABLED)
                                    ->addWhere('wp.widget_id = ?', $widget->id)
                                    ->execute();

                if (!empty($params)) {
                    foreach ($params as $param) {
                        $type = 'string';

                        switch ($param->param_type) {
                            case 'number':
                                $type = 'float';
                                break;

                            case 'string':
                                $type = 'string';
                                break;

                            case 'boolean':
                                $type = 'boolean';
                                break;
                        }

                        $fields[$param->param_key] = array(
                                                            'type'      => $type,
                                                            'fieldLabel'=> $param->param_name,
                                                            'readonly'  => false,
                                                            'fieldname' => $param->param_key,
                                                            'value'     => $param->getValue($admin)
                                                          );
                    }
                } else {
                    $fields[$param->param_key] = array(
                                                        'type'      => 'label',
                                                        'fieldLabel'=> __('widgets~no_params')
                                                      );
                }
            }

            $rep->setResponseContent(array('fields' => array('default' => $fields)));
            return $rep;
        }

        /**
         * Save action
         *
         * @return      Core_Controller_Response_Abstract
         */
        public function saveEdit()
        {
            $admin          = Doctrine_Query::create()
                                ->from('DbAdmin a')
                                ->where('a.id = ?', Core_Authentification::getNamespace()->id)
                                ->fetchOne();

            $widget         = Doctrine_Query::create()
                                ->from('DbWidget w')
                                ->leftJoin('w.DbAdminWidget aw')
                                ->where('w.status = ?', DbBase::ENABLED)
                                ->addWhere('w.enabled = ?', true)
                                ->addWhere('w.id = ?', $this->_intParam('id'))
                                ->addWhere('aw.admin_id = ?', Core_Authentification::getNamespace()->id)
                                ->fetchOne();

            if ($widget) {
                $params         = Doctrine_Query::create()
                                    ->from('DbWidgetParam wp')
                                    ->where('wp.status = ?', DbBase::ENABLED)
                                    ->addWhere('wp.widget_id = ?', $widget->id)
                                    ->execute();

                foreach ($params as $param) {
                    if ($this->_getParam($param->param_key) !== null) {
                        $param->setValue($admin, $this->_getParam($param->param_key));
                    }
                }
            }

            $rep = $this->_getResponse('http');
            $rep->setTemplate('json.tpl');
            $rep->data->assign('json', Zend_Json::encode(array('success' => true, 'errors' => array())));
            return $rep;
        }
    }
